// pages/shop/shop.js
const util=require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      avatarUrl:getApp().globalData.userInfo.avatarUrl,
      nickName:getApp().globalData.userInfo.nickName,
      openSettings:'openPerson',
      initAccount:'initAccount'
    },
    current: 'tab1',
    current1:'tab1',
    tabs: [
      {
        key: 'tab1',
        title: 'Tab 1',
        content: 'Content of tab 1',
      },
      {
        key: 'tab2',
        title: 'Tab 2',
        content: 'Content of tab 2',
      },
      // {
      //   key: 'tab3',
      //   title: 'Tab 3',
      //   content: 'Content of tab 3',
      // },
    ],
    tabs1: [
      {
        key: 'tab1',
        title: 'Tab 1',
        content: ['content1-1','content1-2','content1-3','content1-4','content1-5','content1-6'],
      },
      {
        key: 'tab2',
        title: 'Tab 2',
        content: ['content2-1','content2-2','content2-3','content2-4','content2-5','content2-6'],
      },
      {
        key: 'tab3',
        title: 'Tab 3',
        content: ['content3-1','content3-2','content3-3','content3-4','content3-5','content3-6'],
      },
      {
        key: 'tab4',
        title: 'Tab 4',
        content: ['content4-1','content4-2','content4-3','content4-4','content4-5','content4-6'],
      },
      {
        key: 'tab5',
        title: 'Tab 5',
        content: ['content5-1','content5-2','content5-3','content5-4','content5-5','content5-6'],
      },
      {
        key: 'tab6',
        title: 'Tab 6',
        content: ['content6-1','content6-2','content6-3','content6-4','content6-5','content6-6'],
      }
    ],
    forRollImage://轮播图
    [
        {imgUrl:"https://shoot.photo/images/2022/02/03/q3bF7.png"},
        {imgUrl:"https://shoot.photo/images/2022/02/03/q3bF7.png"},
        {imgUrl:'https://shoot.photo/images/2022/02/03/q3bF7.png'}
    ],
    goodsList:[//商品列表
      {
          _id:0,
          imgurl:'http://cdn.skyvow.cn/logo.png',
          navurl:'',
          goodsInfo:'',
          watching:0,
          discussion:0,
          stars:0
      },
      {
          _id:1,
          imgurl:'http://cdn.skyvow.cn/logo.png',
          navurl:'',
          goodsInfo:'',
          watching:0,
          discussion:0,
          stars:0
      },
      {
          _id:2,
          imgurl:'http://cdn.skyvow.cn/logo.png',
          navurl:'',
          goodsInfo:'',
          watching:0,
          discussion:0,
          stars:0
      },
      {
        _id:3,
        imgurl:'http://cdn.skyvow.cn/logo.png',
        navurl:'',
        goodsInfo:'',
        watching:0,
        discussion:0,
        stars:0
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let app=getApp();
    const db=wx.cloud.database()
    let goods=await db.collection('Goods').where({}).limit(8).get()
    goods=goods.data
    console.log(goods)
    app.globalData.goods=goods
    this.setData({
      pageHeight:app.globalData.windowHeight,
      ['userInfo.avatarUrl']:app.globalData.userInfo.avatarUrl,
      ['userInfo.nickName']:app.globalData.userInfo.nickName,
      ['userInfo.bgc']:app.globalData.presentTheme.lightBgColor,
      presentTheme:app.globalData.presentTheme,
      themeName:app.globalData.presentTheme.deepColor.match(/wux-(\S*)/)[1],
      goodsList:goods,
      
    })
    var j=0;
    var tabs1=[];
    var contents=[];
    for(var i=0;i<goods.length;i++){
      contents.push(goods[i].product_name)
      if((i+1)%3==0){
        tabs1[j]={
          key:"tab"+(j+1),
          title:"推荐"+(j+1),
          content:contents
        }
        console.log(contents)
        contents=[]
        j++
      }
    }
    if(i>=1){
      if(contents!=false){
        tabs1.push({ 
          key:"tab"+(j+1),
          title:"推荐"+(j+1),
          content:contents}
        )
      }
      this.setData({
        tabs1:tabs1,
        tabsHeight:tabs1.length*87<520?tabs1.length*87:520
      }) 
    }
  },
  navToSearch(){
    
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let app=getApp();
    // let that=this;
    // if(app.globalData.hasUserInfo)that.setData({['userInfo.avatarUrl']:app.globalData.userInfo.avatarUrl,['userInfo.nickName']:app.globalData.userInfo.nickName})

    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      current: 2,
      theme:getApp().globalData.presentTheme.midColor.match(/wux-(\S*)/)[1],
      themeName:getApp().globalData.presentTheme.deepColor.match(/wux-(\S*)/)[1],
    })

 }
  },
  //轮播图点击事件
  previewImg:function(res){

    let that=this;
    var img=res.currentTarget.dataset['src']
    wx.previewImage({
      current:img, //当前显示图片的http链接
      urls: [that.data.forRollImage[0].imgUrl,// 需要预览的图片http链接列表
      that.data.forRollImage[1].imgUrl,
      that.data.forRollImage[2].imgUrl] 
    })
},
  onTabsChange(e) {
    // console.log('e.detail', e.detail)
    const { key } = e.detail
    const index = this.data.tabs.map((n) => n.key).indexOf(key)
    this.setData({
      key, //切换活跃颜色
      index, //切换swiper item
    })
  },
  onSubTabsChange(e) {
    // console.log('e.detail', e.detail)
    const { key } = e.detail
    const index1 = this.data.tabs1.map((n) => n.key).indexOf(key)
    this.setData({
      key1:key, //切换活跃颜色
      index1, //切换swiper item
    })
  },
  onSwiperChange(e) {
    // console.log('onSwiperChange', e)
    const { current: index, source } = e.detail
    const { key } = this.data.tabs[index]

    if (!!source) {
      this.setData({
        key,
        index,
      })
    }
  },
  onSubSwiperChange(e) {
    // console.log('onSubSwiperChange', e)
    const { current: index1, source } = e.detail
    const { key } = this.data.tabs1[index1]
    if (!!source) {
      this.setData({
        key1:key,
        index1,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  switchChange(e) {
    // console.log(e.currentTarget.dataset);
    // console.log(e.detail)
    const { model } = e.currentTarget.dataset
    this.setData({
      [model]: e.detail.value,
    })
  }
  ,
  openPerson:function(){
    wx.showToast({
      icon:'none',
      title: '欢迎',
      duration:1000,
      success:()=>{
        wx.switchTab({
          url: '/pages/person/person',
        })
      }
    })
  },
  initAccount:function(){ let app=getApp();
    wx.showToast({
      icon:'none',
      title: '欢迎登录',
      duration:1000,
      success:()=>{
        wx.switchTab({
          url: '/pages/person/person',
        })
      }
    })
    // util.initAccount().then(()=>{
    //   this.setData({['userInfo.avatarUrl']:app.globalData.userInfo.avatarUrl,['userInfo.nickName']:app.globalData.userInfo.nickName})
    // })  
  },
  showGoodsDetail(e){
    console.log(e.currentTarget.dataset.goods)
    // wx.setClipboardData({
    //   data:e.currentTarget.dataset.goods.link,
    //   success (res) {
    //     wx.showToast({
    //       icon:'none',
    //       title: '已复制商品链接！',
    //       duration:2000
    //     })
    //   }
    // })
    wx.navigateTo({
      url: '../shop/showDetail/showDetail',
      success:res=>{
        res.eventChannel.emit('goodsInfo',{ data:e.currentTarget.dataset.goods})
      }
    })
  }
})